<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" inline label-width="100px" :model="tableForm">
            <el-form-item label="工号" prop="mobile" class="width100" style="display: inline-block;">
              <el-input
                v-model="tableForm.mobile"
                type="text"
                placeholder="请输入工号"
                class="selWidth"
                size="small"
                @keyup.enter.native="getRegisterList(1)"
              >
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getRegisterList(1)" />
              </el-input>
            </el-form-item>
            <el-form-item label="时间选择：" class="width100">
              <el-radio-group
                v-model="tableForm.date"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableForm.date)"
              >
                <el-radio-button
                  v-for="(item,i) in fromList.fromTxt"
                  :key="i"
                  :label="item.val"
                >{{ item.text }}</el-radio-button>
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy/MM/dd"
                format="yyyy/MM/dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                placeholder="自定义时间"
                style="width: 250px;"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item label="激活号码" class="width100" style="display: inline-block;">
              <el-input
                v-model="tableForm.phone"
                type="text"
                placeholder="请输入激活号码"
                class="selWidth"
                size="small"
                @keyup.enter.native="getRegisterList(1)"
              >
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getRegisterList(1)" /></el-input>
            </el-form-item>
          </el-form>
          <el-table
            v-loading="listLoading"
            :data="tableData.data"
            style="width: 100%"
            size="mini"
            name="m0"
            :row-key="(row) => { return row.id }"
          >
            <el-table-column prop="uid" label="UId" min-width="60" />
            <el-table-column prop="u_number" label="工号" min-width="150" />
            <el-table-column prop="group_name" label="套餐" min-width="100" />
            <el-table-column prop="mobile" label="激活号码" min-width="100" />
            <el-table-column prop="img" label="图片" min-width="100">
              <template slot-scope="scope">
                <el-image
                  style="width: 36px; height: 36px"
                  :src="scope.row.pic?scope.row.pic:moren"
                  :preview-src-list="[scope.row.pic?scope.row.pic:moren]"
                />
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="开号时间" min-width="100" />
            <el-table-column prop="deposit" label="押金" min-width="50" />
            <el-table-column prop="is_back" label="是否返押" min-width="50">
              <template slot-scope="scope">
                <span v-if="scope.row.is_back=== 0" :style="{color:scope.row.is_back=== 0 ? 'red' : 'back'}">未返还</span>
                <span v-else-if="scope.row.is_back=== 1" :style="{color:scope.row.is_back=== 1 ? 'green' : 'back'}">已返还</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="100" fixed="right">
              <template slot-scope="scope">
                <el-button v-if="scope.row.is_back == 0" type="text" size="small" @click="handleCardStatus(scope.row.id)">确认并且退还押金</el-button>
                <el-button type="text" size="small" style="color: red;" @click="deleteRecord(scope.row.id)">删除</el-button>
                <el-button type="text" size="small" @click="hangdleEdit(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              :page-sizes="[20, 40, 60, 80]"
              :page-size="tableForm.limit"
              :current-page="tableForm.page"
              layout="total, sizes, prev, pager, next, jumper"
              :total="tableData.total"
              @size-change="handleSizeChange"
              @current-change="pageChange"
            />
          </div>
        </div>
      </div>
    </el-card>
    <el-dialog :visible="editVisiable" width="500" @close="closeEdit">
      <el-form ref="ruleForm" :model="editForm" :rules="rules" label-width="100px">
        <el-form-item prop="group_name" label="套餐">
          <el-input v-model="editForm.group_name" />
        </el-form-item>
        <el-form-item prop="mobile" label="激活号码">
          <el-input v-model="editForm.mobile" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeEdit">取 消</el-button>
        <el-button type="primary" @click="submitEdit('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { fromList } from '@/libs/constants.js'
import { getRegisterList, handleCardStatus, handleCardDelete, handleCardEdit } from '@/api/phone'
export default {
  data() {
    const checkPhone = (rule, phone, callback) => {
      var reg = /^1(3|4|5|6|7|8|9)\d{9}$/
      if (reg.test(phone)) {
        callback()
      } else {
        return callback(new Error('请输入正确的手机号'))
      }
    }
    return {
      tableForm: {
        mobile: '',
        date: '',
        phone: '',
        page: 1,
        limit: 20
      },
      tableData: {
        data: [],
        total: 11
      },
      fromList: fromList,
      timeVal: '',
      listLoading: false,
      moren: require('@/assets/images/bjt.png'),
      editVisiable: false,
      editForm: {
        id: '',
        mobile: '',
        group_name: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入激活号码', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }],
        group_name: [{ required: true, message: '请输入套餐', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getRegisterList(1)
  },
  methods: {
    getRegisterList(page) {
      this.tableForm.page = page || this.tableForm.page
      getRegisterList(this.tableForm).then((res) => {
        this.tableData.data = res.data.list
        this.tableData.total = res.data.count
      }).catch((res) => {
        this.$message.error(res.message)
      })
    },
    // 选择时间
    selectChange(tab) {
      this.tableForm.date = tab
      this.tableForm.page = 1
      this.timeVal = []
      this.getRegisterList(1)
    },
    onchangeTime(e) {
      this.timeVal = e
      this.tableForm.date = e ? this.timeVal.join('-') : ''
      this.tableForm.page = 1
      this.getRegisterList(1)
    },
    pageChange(page) {
      this.tableForm.page = page
      this.getRegisterList('')
    },
    handleSizeChange(val) {
      this.tableForm.limit = val
      this.getRegisterList('')
    },
    handleCardStatus(id) {
			  this.$confirm('确认返还押金吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        handleCardStatus({ id }).then((res) => {
          this.$message.success('操作成功')
          this.getRegisterList(1)
        }).catch((res) => {
          this.$message.error(res.message)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    deleteRecord(id) {
      this.$confirm('确认删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        handleCardDelete(id).then((res) => {
          this.$message.success('操作成功')
          this.getRegisterList(1)
        }).catch((res) => {
          this.$message.error(res.message)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    hangdleEdit(row) {
      this.editVisiable = true
      this.editForm.id = row.id
      this.editForm.mobile = row.mobile
      this.editForm.group_name = row.group_name
    },
    closeEdit() {
      this.editVisiable = false
      this.editForm.id = ''
      this.editForm.mobile = ''
      this.editForm.group_name = ''
    },
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          handleCardEdit(this.editForm.id, this.editForm).then(() => {
            this.getRegisterList(1)
          }).catch((e) => {
            this.$message({
              type: 'info',
              message: e
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      //
      })
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
